package com.cc.crm.service.impl;

import com.cc.crm.entity.SysRole;
import com.cc.crm.entity.SysRoleRight;
import com.cc.crm.entity.SysRoleRightExample;
import com.cc.crm.mapper.SysRoleMapper;
import com.cc.crm.mapper.SysRoleRightMapper;
import com.cc.crm.service.IRoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleRightMapper roleRightMapper;
    @Override
    public SysRole get(int roleId) {
        //根据角色id查询该角色的信息以及该角色具有的所有权限的所有信息
        return sysRoleMapper.findRoleAndRights(roleId);
    }

    @Override
    public Page listByPage(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        return (Page) sysRoleMapper.selectByExample(null);
    }

    @Override
    public int deleteRightsWithRole(int roleId) {
        SysRoleRightExample example = new SysRoleRightExample();
        SysRoleRightExample.Criteria criteria = example.createCriteria();
        criteria.andRfRoleIdEqualTo(roleId);
        return roleRightMapper.deleteByExample(example);
    }
    @Transactional
    public void saveRightsWithRole(String[] interest, int roleId) {
        for (int i = 0; i < interest.length; i++) {
            SysRoleRight record = new SysRoleRight();
            record.setRfRightCode(interest[i]);
            record.setRfRoleId(roleId);
            roleRightMapper.insert(record);
        }
    }

}
